package com.yanqu.road.server.cmd.relatives;

import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_RELATIVES_SHOW, desc = "亲家引导")
public class ShowRelativesCmd extends AbstractCommand {
    private long relativesId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RelativesProto.RelativesShowReqMsg reqMsg = RelativesProto.RelativesShowReqMsg.parseFrom(packet.getBytes());
        relativesId = reqMsg.getRelativesId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RelativesInfo relativesInfo = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
        if (relativesInfo == null || relativesInfo.getStatus() == 0) {
            ret = GameErrorCode.E_RELATIVES_TIMES_NOT_ENOUGH;
            return;
        }
        String[] split = relativesInfo.getIsShow().split(",");
        if (player.getUserId() == relativesInfo.getUserId1()) {
            split[0] = "1";
        } else {
            split[1] = "1";
        }
        relativesInfo.setIsShow(split[0]+","+split[1]);
    }

    @Override
    public void sendData(GamePlayer player) {
        RelativesProto.RelativesShowRespMsg.Builder respMsg = RelativesProto.RelativesShowRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_RELATIVES_SHOW, respMsg);
    }
}
